//
//  WBMainViewController.swift
//  传智微博
//
//  Created by sir zhou on 2017/8/30.
//  Copyright © 2017年 sir zhou. All rights reserved.
//

import UIKit

/// 主控制器
class WBMainViewController: UITabBarController {
    
    /// 撰写按钮 私有控件
    lazy var commposeButton: UIButton = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()
        

        setupChildControllers()
        
        setupCommposeButton()
    }
    ////
    
    /// 监听方法
    func composeStatus() {
        print("撰写微博")
    }

}



// extension

extension WBMainViewController{
    
    /// 设置撰写按钮
    func setupCommposeButton() {
        
        
        commposeButton.setImage(UIImage(named: "tabbar_compose_background_icon_add"), for: .normal)
        commposeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
//        commposeButton.setTitle("测试", for: .normal)
        commposeButton.sizeToFit()
        tabBar.addSubview(commposeButton)
        
        // 计算按钮的宽度
        let count = CGFloat(childViewControllers.count)
        let w = tabBar.bounds.width / count - 1
        
        // CGRectInset  正数向内缩进， 负数向外扩展
        commposeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        // 按钮监听方法
        commposeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    /// 设置所有子控制器
    func setupChildControllers(){
        let array = [
            ["clsName": "WBHomeViewController", "title": "首页", "imageName": "home"],
            ["clsName": "WBMessageViewController", "title": "消息", "imageName": "message_center"],
            ["clsName": "", "title": "消息", "imageName": "message_center"],
            ["clsName": "WBDiscoverViewController", "title": "发现", "imageName": "discover"],
            ["clsName": "WBProfileViewController", "title": "我", "imageName": "profile"],
        ]
        
        var arrayM = [UIViewController]()
        for dict in array{
            arrayM.append(controller(dict: dict))
        
        }
        
        viewControllers = arrayM
        
    }
    
    
    
    
    /// 使用字典创建一个自控制器
    
    
    private func controller(dict:[String:String]) -> UIViewController {
        // 1. 取的字典内容
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString("传智微博" + "." + clsName) as? UIViewController.Type
            
        else {
            return UIViewController()
        }
        
        // 2. 创建
        let vc = cls.init()
        
        vc.title = title
        
        
        // 3. 设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        // 4. 设置 tabbar 的标题字体（大小）
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .highlighted)
        
        // 系统默认是 12 号字体， 修改字体大小， 要设置 Normal 的字体大小
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12)], for: .normal)
        
        
        
        let nav = WBNavigationController(rootViewController: vc)
        
        return nav
    }
    
}






























































